package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.ImportDB2ESService;
import com.qingcheng.service.goods.SkuService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImportDB2ESServiceImpl implements ImportDB2ESService {

    @Autowired
    private SkuService skuService;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void importDB() {
        //PageResult<Sku> skuPageResult = skuService.findPage(1, 100);
        // 分批量导入，使用多线程
        int page  = 1;
        while (true) {
            PageResult<Sku> skuPageResult = skuService.findPage(page, 100);
            List<Sku> skuList = skuPageResult.getRows();
            if (skuList.size() > 0){
                this.importDB2es(skuList);
            }
            if(skuPageResult.getRows().size() < 100){
                break;
            }
            page ++;
        }
    }

    private void importDB2es(List<Sku> skuList){

        //2.封装请求对象
        BulkRequest bulkRequest=new  BulkRequest();
        for(Sku sku : skuList) {
            IndexRequest indexRequest=new  IndexRequest("sku","doc");
            Map  skuMap  =new  HashMap();
            skuMap.put("name",sku.getName());
            skuMap.put("id",sku.getId());
            skuMap.put("price",sku.getPrice());
            skuMap.put("image", sku.getImage());
            skuMap.put("images", sku.getImages());
            skuMap.put("weight", sku.getWeight());
            skuMap.put("categoryname", sku.getCategoryName());
            skuMap.put("brandname", sku.getBrandName());
            skuMap.put("spec", sku.getSpec());
            skuMap.put("salenum", sku.getSaleNum());
            skuMap.put("commentnum", sku.getCommentNum());
            indexRequest.source(skuMap);
            bulkRequest.add(indexRequest);//可以多次添加
        }
        //3.获取响应结果
        BulkResponse response  =
                null;
        try {
            response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int  status  =  response.status().getStatus();
        System.out.println(status);
        String  message  =  response.buildFailureMessage();
        System.out.println(message);
    }



   /* public void importDB() {
        PageResult<Sku> skuPageResult = skuService.findPage(1, 100);

        List<Sku> skuList = skuPageResult.getRows();
        //2.封装请求对象
        BulkRequest bulkRequest=new  BulkRequest();
        for(Sku sku : skuList) {
            IndexRequest indexRequest=new  IndexRequest("sku","doc");
            Map  skuMap  =new  HashMap();
            skuMap.put("name",sku.getName());
            skuMap.put("id",sku.getId());
            skuMap.put("price",sku.getPrice());
            skuMap.put("image", sku.getImage());
            skuMap.put("images", sku.getImages());
            skuMap.put("weight", sku.getWeight());
            skuMap.put("categoryname", sku.getCategoryName());
            skuMap.put("brandname", sku.getBrandName());
            skuMap.put("spec", sku.getSpec());
            skuMap.put("salenum", sku.getSaleNum());
            skuMap.put("commentnum", sku.getCommentNum());
            indexRequest.source(skuMap);
            bulkRequest.add(indexRequest);//可以多次添加
        }
        //3.获取响应结果
        BulkResponse response  =
                null;
        try {
            response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int  status  =  response.status().getStatus();
        System.out.println(status);
        String  message  =  response.buildFailureMessage();
        System.out.println(message);



    }*/
}
